package demo18;

import java.lang.annotation.*;
import java.lang.reflect.Field;

public class Test{
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
        Class c1 = Class.forName("demo18.Test2");
        Annotation[] annotations = c1.getAnnotations();
        for (Annotation annotation : annotations) {
            System.out.println(annotation);
        }
        Tablekuang tablekuang = (Tablekuang)c1.getAnnotation(Tablekuang.class);
        String value = tablekuang.value();
        System.out.println(value);

        Field f = c1.getDeclaredField("name");
        Fieldkuang annotation = f.getAnnotation(Fieldkuang.class);
        System.out.println(annotation.columnName());
        System.out.println(annotation.type());
        System.out.println(annotation.length());

    }

}
@Tablekuang("db_student")
class Test2 {
    @Fieldkuang(columnName = "student1",type="int",length=10)
    private int id;
    @Fieldkuang(columnName = "student2",type="int",length=10)
    private int age;
    @Fieldkuang(columnName = "student3",type="varchar",length=10)
    private String name;
    public Test2(){

    }

    public Test2(int id, int age, String name) {
        this.id = id;
        this.age = age;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Test{" +
                "id=" + id +
                ", age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}

@Target(ElementType.TYPE)
//生命周期
@Retention(RetentionPolicy.RUNTIME)
@interface Tablekuang{
    //下面的这个就是我们的注解，而不是方法
    String value();
}

//下面的这个是对于属性的注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface Fieldkuang{
    //下面的这个就是我们的注解，而不是方法
    String columnName();
    String type();
    int length();

}
